package com.sgcc.lms.openstack.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sgcc.lms.openstack.exception.*;
import com.sgcc.lms.openstack.service.CinderService;
import com.sgcc.lms.openstack.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.storage.block.Volume;
import org.openstack4j.model.storage.block.VolumeType;
import org.openstack4j.openstack.storage.block.domain.CinderVolume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-22 13:55
 * @Description: 块存储服务控制层
 */
@RestController
@Slf4j
@Api(tags = "CinderController：块存储服务")
public class CinderController {
    @Autowired
    private CinderService cinderService;
    private static final String LINE = "==========================================》{}";

    /**
     * @Author 李孟帅
     * @Description 列出卷类型
     * @Date 2020/7/22
     **/
    @GetMapping("/volumeTypes")
    @ApiOperation("列出卷类型")
    public ResultUtil<?> volumeTypes(HttpServletRequest request) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends VolumeType> volumeTypes = cinderService.volumeTypes(tokenId);
        log.info(uri+",列出卷类型"+LINE,"成功");
        return ResultUtil.success(volumeTypes);
    }

    /**
     * @Author 李孟帅
     * @Description 列出当前项目有权访问的卷
     * @Date 2020/7/22
     **/
    @GetMapping("/volumes")
    @ApiOperation("列出当前项目有权访问的卷")
    public ResultUtil<?> volumes(HttpServletRequest request) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Volume> volumes = cinderService.volumes(tokenId);
        log.info(uri+",列出当前项目有权访问的卷"+LINE,"成功");
        return ResultUtil.success(volumes);
    }

    /**
     * @Author 李孟帅
     * @Description 获取卷
     * @Date 2020/7/22
     **/
    @GetMapping("/volumes/get")
    @ApiOperation("获取卷")
    public ResultUtil<?> getVolume(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Volume volume = cinderService.getVolume(tokenId, id);
        log.info(uri+",获取卷"+LINE,"成功");
        return ResultUtil.success(volume);
    }

    /**
     * @Author 李孟帅
     * @Description 创建卷
     * @Date 2020/7/22
     **/
    @PostMapping("/volumes/create")
    @ApiOperation("创建卷")
    public ResultUtil<?> createVolume(HttpServletRequest request, @RequestBody CinderVolume volume) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Volume volume1 = cinderService.createVolume(tokenId, volume);
        log.info(uri+",创建卷"+LINE,"成功");
        return ResultUtil.success(volume1);
    }

    /**
     * @Author 李孟帅
     * @Description 更新卷
     * @Date 2020/7/22
     **/
    @PostMapping("/volumes/update")
    @ApiOperation("更新卷")
    public ResultUtil<?> updateVolume(HttpServletRequest request, @RequestBody CinderVolume volume) throws  IOException, NullTokenException, HttpException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        String result = cinderService.updateVolume(tokenId, volume);
        JSONObject jsonObject = JSON.parseObject(result);
        log.info(uri+",更新卷"+LINE,"成功");
        return ResultUtil.success(jsonObject);
    }

    /**
     * @Author 李孟帅
     * @Description 删除卷
     * @Date 2020/7/22
     **/
    @GetMapping("/volumes/delete")
    @ApiOperation("删除卷")
    public ResultUtil<?> deleteVolume(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse volume = cinderService.deleteVolume(tokenId, id);
        log.info(uri+",删除卷"+LINE,"成功");
        return ResultUtil.success(volume);
    }
}
